package cn.gson.boot.model.service;

import cn.gson.boot.model.dao.*;
import cn.gson.boot.model.mapper.AttributeMapper;
import cn.gson.boot.model.mapper.PurchaseDetailsMapper;
import cn.gson.boot.model.mapper.PurchaseMapper;
import cn.gson.boot.model.pojos.*;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class PurchaseService {
    @Autowired
    PurchaseMapper purchaseMapper;
    @Autowired
    PurchaseDao purchaseDao;
    @Autowired
    PurchaseDetailsDao purchaseDetailsDao;
    @Autowired
    PurchaseDetailsMapper purchaseDetailsMapper;
    @Autowired
    ShenHeDao shenHeDao;

    //采购单分页查询
    public List<Purchase> selectPurchases(String search, String type) {
        return purchaseMapper.selectPurchases(search,type);
    }

    //查询所有的供应商
    public List<Supplier> selectSupplier() {
        return purchaseMapper.selectSupplier();
    }

    //采购模糊查询供应商
    public List<Supplier> SelectBySupplierName(String supplierName) {
        return purchaseMapper.SelectBySupplierName(supplierName);
    }

    //根据供应商名称查询供应商联系人
    public List<SupplierContact> ContactBySupplierName(String supplierName) {
        return purchaseMapper.ContactBySupplierName(supplierName);
    }

    //查询单号
    public String PurchaseOrder() {
        SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMdd");
        Date d = new Date();
        String code = purchaseMapper.PurchaseOrder();
        if(null==code || "".equals(code)) {
            code = "CG"+sdf.format(d)+"1";
        }else {
            int zhi = Integer.parseInt(code)+1;
            code = "CG"+sdf.format(d)+ zhi +"";
        }
        return code;
    }

    public void addPurchase(Purchase purchase, List purList) {
        if(purchase.getPurchaseId()==0) {
            JSONArray json = JSONArray.parseArray(String.valueOf(purList));
            purchase.setPurchaseSc(0);
            purchase.setPurchaseStatus("待审核");
            purchaseDao.save(purchase);
            Shenhe shenhe = new Shenhe();
            shenhe.setShTabid(purchase.getPurchaseId());
            shenhe.setShShouming("采购");
            shenhe.setShZhuangtai("待审核");
            shenhe.setEmp(null);
            shenhe.setChuangJiantime(new Timestamp(new Date().getTime()));
            shenhe.setShenqingPeople(purchase.getPurchaseMan());
            shenHeDao.save(shenhe);
            PurchaseDetails purchaseDetails = null;
            JSONObject ob = null;
            Productt productt = null;
            for (int i = 0; i < json.size(); i++) {
                ob = (JSONObject) json.get(i);//得到json对象
                Integer purdetailsId = ob.getInteger("purdetailsId");
                Integer producttId = ob.getInteger("productId");
                Integer purNumber = ob.getInteger("purNumber");
                BigDecimal purPrice = ob.getBigDecimal("purPrice");
                productt = new Productt();
                productt.setProducttId(producttId);
                purchaseDetails = new PurchaseDetails();
                if (purdetailsId != null) {
                    purchaseDetails.setPurdetailsId(purdetailsId);
                }
                purchaseDetails.setProducttId(productt);
                purchaseDetails.setPurdetailsNumber(purNumber);
                purchaseDetails.setPurdetailsPrice(new BigDecimal(String.valueOf(purPrice.multiply(BigDecimal.valueOf(purNumber)))));
                purchaseDetails.setPurchaseId(purchase);
                purchaseDetailsDao.save(purchaseDetails);
            }
        }else{
            JSONArray json = JSONArray.parseArray(String.valueOf(purList));
            purchase.setPurchaseSc(0);
            purchaseDao.save(purchase);
            purchaseDetailsDao.deleteAll(purchaseDetailsMapper.selectPurchaseDetails(purchase.getPurchaseId()));
            PurchaseDetails purchaseDetails = null;
            JSONObject ob = null;
            Productt productt = null;
            for (int i = 0; i < json.size(); i++) {
                ob = (JSONObject) json.get(i);//得到json对象
                Integer purdetailsId = ob.getInteger("purdetailsId");
                Integer producttId = ob.getInteger("productId");
                Integer purNumber = ob.getInteger("purNumber");
                BigDecimal purPrice = ob.getBigDecimal("purPrice");
                productt = new Productt();
                productt.setProducttId(producttId);
                purchaseDetails = new PurchaseDetails();
                if (purdetailsId != null) {
                    purchaseDetails.setPurdetailsId(purdetailsId);
                }
                purchaseDetails.setProducttId(productt);
                purchaseDetails.setPurdetailsNumber(purNumber);
                purchaseDetails.setPurdetailsPrice(new BigDecimal(String.valueOf(purPrice.multiply(BigDecimal.valueOf(purNumber)))));
                purchaseDetails.setPurchaseId(purchase);
                purchaseDetailsDao.save(purchaseDetails);
            }
        }
    }

    //查询需要修改的采购详情
    public List<PurchaseDetails> selectPurchaseDetails(Integer purchaseId) {
        return purchaseDetailsMapper.selectPurchaseDetails(purchaseId);
    }

    //查询采购单状态
    public String PurchaseStatus(Integer purchaseId) {
        return purchaseMapper.PurchaseStatus(purchaseId);
    }

    //删除采购单
    public void delPurchase(Integer purchaseId) {
        Purchase purchase = purchaseDao.findById(purchaseId).get();
        purchase.setPurchaseSc(1);
    }
}
